﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Web.Mvc;
using System.Xml.Linq;
using Xenta.Attributes;
using Xenta.Utils;

namespace Xenta.Web.Controllers
{
    public sealed class NewsController : ExtController
    {
        #region GET/POST Actions

        [HttpGet]
        public ActionResult Index(string searchTerm,
            string dtFrom,
            string dtTo, 
            int? page)
        {
            page = page.HasValue ? page.Value : 0;
            var dtNow = DateTime.UtcNow;
            var publishedFrom = String.IsNullOrWhiteSpace(dtFrom)
                ? (DateTime?)null
                : Svc.Date(dtFrom);
            var publishedTo = String.IsNullOrWhiteSpace(dtTo)
                ? dtNow
                : Svc.Date(dtTo);
            if(publishedTo > dtNow)
                publishedTo = dtNow;
            dynamic m = new ExpandoObject();

            m.CountPerPage = Svc.Config(Par.In("WebPortal").Of(this).Group("Index").Name("Count"), 10);
            m.Index = Svc.ProcessOperation("SearchPublications", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                Term = new
                {
                    Expression = String.IsNullOrWhiteSpace(searchTerm) ?
                        String.Empty :
                        String.Format("%{0}%", searchTerm)
                },
                Index = page * m.CountPerPage,
                Count = m.CountPerPage,
                PublishedFrom = publishedFrom,
                PublishedTo = publishedTo,
                FlagMask = "Active,News",
                SortBy = new[] 
                {
                    "PublishedOn"
                },
                SortDir = "Descending"
            }, new
            {
                DisclosureLevel = "Extended",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            return View(m);
        }

        [HttpGet, RestoreState]
        public ActionResult Show(int id)
        {
            var m = Svc.ProcessOperation("GetPublication", new
            {
                EntityID = id
            }, new
            {
                DisclosureLevel = "Entire",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            if(m == null ||
                !JDM.HasFlags(m.Flags, "Active") ||
                !JDM.HasFlags(m.Flags, "News") ||
                JDM.DateTime(m.PublishedOn) > DateTime.UtcNow)
            {
                return HttpNotFound();
            }
            return View(m);
        }

        #endregion

        #region Child Actions

        [ChildActionOnly]
        public ActionResult _Archive(int? year, 
            int? month, 
            int? day, 
            int? period)
        {
            string rptMimeType = String.Empty;
            long rptElapsedTime = 0;
            dynamic m = new ExpandoObject();
            m.Error = String.Empty;
            m.Data = null;

            try
            {
                using(var rptData = Svc.BuildReport("PublicationCountByMonthReport",
                    new Dictionary<String, String>
                    {
                        {"languageID", JDM.String(Svc.Localization.Language.EntityID)},
                        {"year", year.ToString()},
                        {"month", month.ToString()},
                        {"day", day.ToString()},
                        {"period", period.ToString()},
                        {"flagMask", "Active,News"}
                    },
                    out rptMimeType,
                    out rptElapsedTime))
                {
                    m.Data = XElement.Load(rptData);
                }
            }
            catch(Exception ex)
            {
                m.Error = ex.ToString();
            }
            m.ElapsedTime = rptElapsedTime;

            return PartialView(m);
        }

        [ChildActionOnly]
        public ActionResult _Latest()
        {
            var m = Svc.ProcessOperation("SearchPublications", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                Count = Svc.Config(Par.In("WebPortal").Of(this).Group("_Latest").Name("Count"), 10),
                PublishedTo = DateTime.UtcNow,
                FlagMask = "Active,News",
                SortBy = new[] 
                {
                    "PublishedOn"
                },
                SortDir = "Descending"
            }, new
            {
                DisclosureLevel = "Base",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            return PartialView(m);
        }

        #endregion
    }
}
